package com.ta.gulimall.coupon.service.impl;

import cn.hutool.core.lang.TypeReference;
import com.ta.gulimall.coupon.feign.ProductFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ta.gulimall.common.utils.system.PageUtils;
import com.ta.gulimall.common.utils.system.Query;

import com.ta.gulimall.coupon.dao.SpuBoundsDao;
import com.ta.gulimall.coupon.entity.SpuBoundsEntity;
import com.ta.gulimall.coupon.service.SpuBoundsService;


@Service("spuBoundsService")
public class SpuBoundsServiceImpl extends ServiceImpl<SpuBoundsDao, SpuBoundsEntity> implements SpuBoundsService {
    @Autowired
    ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuBoundsEntity> page = this.page(
                new Query<SpuBoundsEntity>().getPage(params),
                new QueryWrapper<SpuBoundsEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Map<Long, SpuBoundsEntity> listSpuBoundBySkuIds(List<Long> skuIds) {
        Map<Long, Long> skuIdMap = productFeignService.listSpuIdsBySkuIds(skuIds).getData(new TypeReference<Map<Long, Long>>() {});
        Map<Long, SpuBoundsEntity> entityMap = baseMapper.listSpuBoundBySkuIds(skuIdMap.values());

        return skuIdMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, v -> entityMap.get(v.getValue())));
    }

}